package List.set;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/4
 * //练习2.定义一个Person数组，将Person数组中的重复对象剔除
 * ===》不允许重复存放元素,Person重写equals()和hashCode()
 */
public class Test02 {
    public static void main(String[] args) {
        Person[] persons = {
                new Person("张三", 20),
                new Person("李四", 25),
                new Person("张三", 20),
                new Person("王五", 30),
        };
        HashSet<Person> hs = new HashSet<>();
        hs.addAll(Arrays.asList(persons));
        System.out.println(hs);
    }
}
class Person {
    private String name;
    private int age;


    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Person{name = " + name + ", age = " + age + "}";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

